/**
 * 常用工具类
 */
export default class Tool {
	/**
	 * 数组查找对象索引
	 * @param arr 数组
	 * @param val 查找对象
	 * @param compareFunc 比较器
	 */
	public static arrayIndexOf<T>(arr: Array<T>, val: T, compareFunc: (arg0: T, arg1: T) => boolean): number {
		if (!arr) {
			return -1;
		}
		if (!compareFunc) {
			for (let i = 0; i < arr.length; i++) {
				if (arr[i] === val) return i;
			}
		} else {
			for (let i = 0; i < arr.length; i++) {
				if (compareFunc(arr[i], val)) return i;
			}
		}
		return -1;
	}
	/**
	 * 删除数组中的对象 按值
	 */
	public static arrayRemoveByValue<T>(arr: Array<T>, value: T) {
		let index = arr.indexOf(value);
		if (index > -1) {
			arr.splice(index, 1);
		}
	}
	//-- 当前字段是否为空
	public static isNull(value:any) {
		if (typeof value == "undefined") {
			return true;
		}
		if (value == null) {
			return true;
		}
		return false;
	}
	//-- 是数组
	public static isArray(obj:any) {
		if (this.isNull(obj)) {
			return false;
		}
		return obj instanceof Array;
	}
	public static isNumber(obj:any): boolean {
		if (this.isNull(obj)) {
			return false;
		}
		return typeof obj == "number";
	}
	//-- 是函数
	public static isFunction(obj:any) {
		if (this.isNull(obj)) {
			return false;
		}
		return obj instanceof Function;
	}
	/** 将字符串形式的 大于小于等于号 翻译之后对比大小 */
	public static getCheckWayResult(checkWay:string,v1:number,v2:number):boolean{
		if (checkWay == ">") {
			return v1 > v2;
		}else if(checkWay == "=="){
			return v1 == v2;
		}else if(checkWay == ">="){
			return v1 >= v2;
		}else if(checkWay == "<="){
			return v1 <= v2;
		}else if(checkWay == "<"){
			return v1 < v2;
		}
		return false;
	}
	/**
	 * 保留小数点
	 * @param num 
	 * @param fixed 
	 */
	public static fixedNum(num: number, fixed: number): number {
		let step = Math.pow(10, fixed);
		return Math.floor(num * step) / step;
	}
	
	/*************************************************************************************************
	 * 校验
	 ************************************************************************************************/
	public static formatPhoneNumber(phoneNumber: string) {
		return phoneNumber.replace(/(\d{3})\d{4}(\d{4})$/, "$1****$2");
	}
	public static verifyPhoneNumber(phoneNumber: string) {
		return new RegExp(/^1[3456789]\d{9}$/).test(phoneNumber);
	}
	public static verifyPassword(password: string) {
		return new RegExp(/^(?![a-zA-Z]+$)(?!\d+$)(?![\W_]+$)[a-zA-Z\d`~!@#$%^&*()_+-={}|\[\]\\:";'<>?,./]{6,16}$/).test(password);
	}
	public static verifyRoleName(nickName: string) {
		return new RegExp(/^[a-zA-Z0-9\u4e00-\u9fa5]+$/).test(nickName);
	}
	public static verifyEmail(email: string) {
		return new RegExp(/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/).test(email);
	}
	/**
	 * @ 前字符长度<10正常显示，>=10截取前2位+后2位中间用 *** 代替
	 * 12345679123@163.com.cn截取后变为:12***23@163.com.cn
	 * @param email 
	 * @returns 
	 */
	public static formatEmail(email: string) {
		let pointAt = email.indexOf("@");
		if (pointAt >= 10) {
			return email.substring(0, 2) + "***" + email.substring(pointAt - 2, email.length)
		}
		return email;
	}
	/*************************************************************************************************
	 * 本地数据存取
	 ************************************************************************************************/
	public static setItem(e: string, t: string) {
		try {
			uni.setStorageSync(e, t);
		} catch (error) {
			console.error(error);
		}
	}
	public static getItem(e: string) {
		return uni.getStorageSync(e);
	}
	public static setString(e: string, t: string) {
		Tool.setItem(e, t);
	}
	public static getString(e: string, d?: string): string {
		var t = Tool.getItem(e);
		return t || d || "";
	}
	public static setNumber(e: string, t: number) {
		Tool.setItem(e, t.toString());
	}
	public static getNumber(e: string, d?: number): number {
		var t = Tool.getItem(e);
		return t ? Number(t) : (d || 0);
	}
	public static setBool(e: string, t: boolean) {
		t ? Tool.setItem(e, "1") : Tool.setItem(e, "0");
	}
	public static getBool(e: string, b?: boolean): boolean {
		var t = Tool.getItem(e);
		if (t == null) {
			if (b != undefined) {
				return b
			}
		}
		return !!t && 1 == Number(t);
	}
	public static setObject(e: string, t: any) {
		t && null != t && Tool.setString(e, JSON.stringify(t))
	}
	public static getObject(e: string): any {
		var t = Tool.getString(e);
		return t && "" != t ? JSON.parse(t) : null;
	}
	public static clearData() {
		uni.clearStorageSync();
	}
	public static removeItem(e: string) {
		uni.removeStorageSync(e);
	}
	/**
	 * @param netData
	 * @param data
	 */
	public static buildInitProp(netData:any,data:any){
		Object.keys(netData).forEach(key=>{
			data[key]=netData[key];
		})
		return data;
	}
}